//JavaScript代码区域
layui.use(['layer', 'form', 'table'], function() {
	var layer = layui.layer,
		$ = layui.$,
		form = layui.form,
		table = layui.table;

	$.ajax({
		url:'../css/menu.json',
		type: 'GET',
		datatype : 'json',
		async: false,
		success: function(result, status, xhr) {
			layer.closeAll('loading');
			menus = result.data;
			$.each(menus, function(idx, obj) {
				var type = "按钮";
				var href = obj.href;
				var ico = obj.ico;
				if(obj.type == 1) {
					type = "菜单"
				}
				if(ico == null) {
					ico = ""
				}
				if(href == null || href==""){
					href = ""
					var html = "<tr data-tt-id='" + obj.id + "' data-tt-parent-id='" + obj.parentid + "'>" +
					"<td>" + obj.name + "</td>" +
					"<td>" + ico + "</td>" +
					"<td>" + href + "</td>" +
					"<td>" + type + "</td>" +
					"<td>" + obj.permission + "</td>" +
					"<td>" + obj.sort + "</td>" +
					"<td></td>" +
					"<td><button class='layui-btn layui-btn-xs edit' data-id='" + obj.id + "'>编辑</button><button class='layui-btn layui-btn-xs layui-btn-danger delete' data-id='" + obj.id + "'>删除</button></td>" +
					"</tr>";
				}else{
					var html = "<tr data-tt-id='" + obj.id + "' data-tt-parent-id='" + obj.parentid + "'>" +
					"<td>" + obj.name + "</td>" +
					"<td>" + ico + "</td>" +
					"<td>" + href + "</td>" +
					"<td>" + type + "</td>" +
					"<td>" + obj.permission + "</td>" +
					"<td>" + obj.sort + "</td>" +
					"<td></td>" +
					"<td><button class='layui-btn layui-btn-xs edit' data-id='" + obj.id + "'>添加</button><button class='layui-btn layui-btn-xs edit' data-id='" + obj.id + "'>编辑</button><button class='layui-btn layui-btn-xs layui-btn-danger delete' data-id='" + obj.id + "'>删除</button></td>" +
					"</tr>";
				}
				
				$('#menus').children('tbody').append(html);
			});
		}
	});
	initTree();
	
	/*添加后台菜单*/
	$("#btnaddmenu").click(function(){
		var index=layer.open({
			type :1,
			title : '后台菜单添加',
			area : ['830px', '95%'],
			shade : 0.4,
			maxmin: true,
			content : $('#addMenuLayer'),
		});
		layer.full(index);
		
	});
})

function initTree() {
	$("#menus").treetable({
		expandable: true
	});
}